﻿using MediatR;
using TraceabilityApp_Api.Application.Command.Breed_养殖_;
using TraceabilityApp_Api.DTO;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.Breed_养殖_
{
    public class GetFirmHandler : IRequestHandler<GetFirmCommand, ApiResult<FirmDTO>>
    {
        private readonly IBase<FirmModel> firmRepo;

        public GetFirmHandler(IBase<FirmModel> firmRepo)
        {
            this.firmRepo = firmRepo;
        }
        public Task<ApiResult<FirmDTO>> Handle(GetFirmCommand request, CancellationToken cancellationToken)
        {
            ApiResult<FirmDTO> result=new ApiResult<FirmDTO>();
            var firm = firmRepo.GetAll().Where(x => !x.IsDeleted);
            if(!string.IsNullOrEmpty(request.Batch))
            {
                firm = firm.Where(x=>x.Batch.Contains(request.Batch));
            }    
            var linq=(from a in firm
                      select new FirmDTO
                      {
                          Batch = a.Batch,
                          Herd=a.Herd,  
                          Livestock=a.Livestock,
                          ResponsibleName=a.ResponsibleName,
                      }).FirstOrDefault(x=>x.Batch==request.Batch);
            result.Data = linq;
            result.Code=ApiEnum.OK;
            result.Msg = "养殖企业显示成功";
            return Task.FromResult(result);
        }
    }
}
